package com.ruoyi.project.hcy.product.service.impl;

import com.ruoyi.common.exception.hcy.product.IllegalCountException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.hcy.product.domain.style.ProStyle;
import com.ruoyi.project.hcy.product.mapper.ProStyleMapper;
import com.ruoyi.project.hcy.product.mapper.ProductMapper;
import com.ruoyi.project.hcy.product.service.IProStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ProStyleServiceImpl
 * @Author SAI
 * @Date 2020/9/18 8:49
 * @Description ProStyleServiceImpl
 * @Version 1.0
 */
@Service("style")
public class ProStyleServiceImpl implements IProStyleService {

    @Autowired
    private ProStyleMapper proStyleMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProStyle> selectProStyleList(ProStyle proStyle) {
        return proStyleMapper.selectProStyleList(proStyle);
    }

    @Override
    public String checkStyleNameUnique(ProStyle proStyle) {
        Long styleId = StringUtils.isNull(proStyle.getStyleId()) ? -1L : proStyle.getStyleId();
        ProStyle info = proStyleMapper.checkStyleNameUnique(proStyle.getStyleName());
        if (StringUtils.isNotNull(info) && info.getStyleId().longValue() != styleId.longValue()) {
            return "1";
        }
        return "0";
    }

    @Override
    public int insertProStyle(ProStyle proStyle) {
        return proStyleMapper.insertProStyle(proStyle);
    }

    @Override
    public ProStyle selectProStyleById(Long styleId) {
        return proStyleMapper.selectProStyleById(styleId);
    }

    @Override
    public int updateProStyle(ProStyle proStyle) {
        return proStyleMapper.updateProStyle(proStyle);
    }

    @Override
    public int deleteProStyleByIds(String ids) {
        Long[] styleIds = Convert.toLongArray(ids);
        for (Long styleId : styleIds) {
            if (productMapper.countStyleProductById(styleId) > 0) {
                ProStyle proStyle = proStyleMapper.selectProStyleById(styleId);
                throw new IllegalCountException(String.format("%1$s已被产品使用,不能删除", proStyle.getStyleName()));
            }
        }
        return proStyleMapper.delectProStyleByIds(styleIds);
    }
}
